

//
//  PictureBackgroundView.m
//  PictureShower
//
//  Created by 宫 酉昊 on 15/6/2.
//  Copyright (c) 2015年 宫 酉昊. All rights reserved.
//

#import "PictureBackgroundView.h"

@implementation PictureBackgroundView

-(PictureBackgroundView *)initWithFrame:(CGRect)cgrect andImageView:(UIImageView *)imageView{
    if (self = [super init]) {
        self.alwaysBounceHorizontal = NO;
        self.alwaysBounceVertical = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.frame = cgrect;
        self.image = imageView.image;
        UIWindow *window=[UIApplication sharedApplication].keyWindow;
        CGRect oldframe = [imageView convertRect:imageView.bounds toView:window];
        self.delegate=self;
        //设置最大伸缩比例
        self.maximumZoomScale=2.0;
        //设置最小伸缩比例
        self.minimumZoomScale=0.5;
        
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:oldframe];
        imageView.image=self.image;
        imageView.tag=1;
        imageView.transform = CGAffineTransformMakeRotation(M_PI*2);
        self.imageView = imageView;
        [self addSubview:imageView];
        
        if (self.image.size.height >= [UIScreen mainScreen].bounds.size.height) {
            CGSize size = self.contentSize;
            size.height = imageView.image.size.height;
            self.contentSize = size;
            [UIView animateWithDuration:0.3 animations:^{
                imageView.frame=CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, self.image.size.height*[UIScreen mainScreen].bounds.size.width/self.image.size.width);
                self.alpha=1;
            } completion:^(BOOL finished) {
                
            }];
        }else{
            [UIView animateWithDuration:0.3 animations:^{
                imageView.frame=CGRectMake(0,([UIScreen mainScreen].bounds.size.height-self.image.size.height*[UIScreen mainScreen].bounds.size.width/self.image.size.width)/2, [UIScreen mainScreen].bounds.size.width, self.image.size.height*[UIScreen mainScreen].bounds.size.width/self.image.size.width);
                self.alpha=1;
            } completion:^(BOOL finished) {
            }];
        }
    }
    return self;
}


-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return self.imageView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{//缩放后重新定位居中
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;
    self.imageView.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end